﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Data.Linq;

namespace KTSonline.ajax
{
    public partial class ListProduct : System.Web.UI.Page
    {
        public Boolean hasItems = true;

        public String spmoi(object b)
        {
            if (Boolean.Parse(b.ToString()) == true)
                return "<img class='new' src='../images/templates/default/new.gif' alt='new' />";
            return "";
        }
        public String sphot(object b)
        {
            if (Boolean.Parse(b.ToString()) == true)
                return "<img class='hot' src='../images/templates/default/hot.gif' alt='hot' />";
            return "";
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            if (Request.QueryString["mode"] != null)
            {
                int mode = Int32.Parse(Request.QueryString["mode"]);
                KTSonlineDataContext db = new KTSonlineDataContext();
                var result = db.spa_LayDanhSachSP(mode).ToList();
                if (result.Count == 0)
                    hasItems = false;
                ListSP.DataSource = result;
                ListSP.DataBind();
            }

            if (Request.QueryString["search"] != null)
            {
                int mode = Int32.Parse(Request.QueryString["search"]);
                string name = Request.QueryString["name"].ToLower();
                int supplier = Int32.Parse(Request.QueryString["supplier"]);
                int pfrom = Int32.Parse(Request.QueryString["pfrom"]);
                int pto = Int32.Parse(Request.QueryString["pto"]);
                
                KTSonlineDataContext db = new KTSonlineDataContext();
                System.Collections.Generic.List<spa_LayDanhSachSPResult> result = new System.Collections.Generic.List<spa_LayDanhSachSPResult>();
                
                switch (mode)
                {
                    case 1:
                        if(supplier != 0)
                            result = db.MAY_ANHs.Where(d => d.nhasanxuat == supplier)
                            .Select(d => new spa_LayDanhSachSPResult { 
                                id = d.ID, 
                                giaban = d.giaban, 
                                sanphamhot = d.sanphamhot, 
                                sanphammoi = d.sanphammoi, 
                                tenmay = d.tenmay, 
                                thumbnail = d.thumbnail}).ToList();
                        else
                            result = db.MAY_ANHs
                            .Select(d => new spa_LayDanhSachSPResult
                            {
                                id = d.ID,
                                giaban = d.giaban,
                                sanphamhot = d.sanphamhot,
                                sanphammoi = d.sanphammoi,
                                tenmay = d.tenmay,
                                thumbnail = d.thumbnail
                            }).ToList();
                        break;
                    case 2:
                        if (supplier != 0)
                            result = db.MAY_QUAY_PHIMs.Where(d => d.nhasanxuat == supplier)
                            .Select(d => new spa_LayDanhSachSPResult
                            {
                                id = d.ID,
                                giaban = d.giaban,
                                sanphamhot = d.sanphamhot,
                                sanphammoi = d.sanphammoi,
                                tenmay = d.tenmay,
                                thumbnail = d.thumbnail
                            }).ToList();
                        else
                            result = db.MAY_QUAY_PHIMs
                            .Select(d => new spa_LayDanhSachSPResult
                            {
                                id = d.ID,
                                giaban = d.giaban,
                                sanphamhot = d.sanphamhot,
                                sanphammoi = d.sanphammoi,
                                tenmay = d.tenmay,
                                thumbnail = d.thumbnail
                            }).ToList();
                        break;
                    case 3:
                        if (supplier != 0)
                            result = db.MAY_NGHE_NHACs.Where(d => d.nhasanxuat == supplier)
                            .Select(d => new spa_LayDanhSachSPResult
                            {
                                id = d.ID,
                                giaban = d.giaban,
                                sanphamhot = d.sanphamhot,
                                sanphammoi = d.sanphammoi,
                                tenmay = d.tenmay,
                                thumbnail = d.thumbnail
                            }).ToList();
                        else
                            result = db.MAY_NGHE_NHACs
                            .Select(d => new spa_LayDanhSachSPResult
                            {
                                id = d.ID,
                                giaban = d.giaban,
                                sanphamhot = d.sanphamhot,
                                sanphammoi = d.sanphammoi,
                                tenmay = d.tenmay,
                                thumbnail = d.thumbnail
                            }).ToList();
                        break;
                    case 4:
                        if (supplier != 0)
                            result = db.LOAs.Where(d => d.nhasanxuat == supplier)
                            .Select(d => new spa_LayDanhSachSPResult
                            {
                                id = d.ID,
                                giaban = d.giaban,
                                sanphamhot = d.sanphamhot,
                                sanphammoi = d.sanphammoi,
                                tenmay = d.tenmay,
                                thumbnail = d.thumbnail
                            }).ToList();
                        else
                            result = db.LOAs
                            .Select(d => new spa_LayDanhSachSPResult
                            {
                                id = d.ID,
                                giaban = d.giaban,
                                sanphamhot = d.sanphamhot,
                                sanphammoi = d.sanphammoi,
                                tenmay = d.tenmay,
                                thumbnail = d.thumbnail
                            }).ToList();
                        break;
                    case 5:
                        if (supplier != 0)
                            result = db.TAI_NGHEs.Where(d => d.nhasanxuat == supplier)
                            .Select(d => new spa_LayDanhSachSPResult
                            {
                                id = d.ID,
                                giaban = d.giaban,
                                sanphamhot = d.sanphamhot,
                                sanphammoi = d.sanphammoi,
                                tenmay = d.tenmay,
                                thumbnail = d.thumbnail
                            }).ToList();
                        else
                            result = db.TAI_NGHEs
                            .Select(d => new spa_LayDanhSachSPResult
                            {
                                id = d.ID,
                                giaban = d.giaban,
                                sanphamhot = d.sanphamhot,
                                sanphammoi = d.sanphammoi,
                                tenmay = d.tenmay,
                                thumbnail = d.thumbnail
                            }).ToList();
                        break;
                    case 6:
                        if (supplier != 0)
                            result = db.MAY_TINH_BANGs.Where(d => d.nhasanxuat == supplier)
                            .Select(d => new spa_LayDanhSachSPResult
                            {
                                id = d.ID,
                                giaban = d.giaban,
                                sanphamhot = d.sanphamhot,
                                sanphammoi = d.sanphammoi,
                                tenmay = d.tenmay,
                                thumbnail = d.thumbnail
                            }).ToList();
                        else
                            result = db.MAY_TINH_BANGs
                            .Select(d => new spa_LayDanhSachSPResult
                            {
                                id = d.ID,
                                giaban = d.giaban,
                                sanphamhot = d.sanphamhot,
                                sanphammoi = d.sanphammoi,
                                tenmay = d.tenmay,
                                thumbnail = d.thumbnail
                            }).ToList();
                        break;
                    case 7:
                        if (supplier != 0)
                            result = db.O_CUNGs.Where(d => d.nhasanxuat == supplier)
                            .Select(d => new spa_LayDanhSachSPResult
                            {
                                id = d.ID,
                                giaban = d.giaban,
                                sanphamhot = d.sanphamhot,
                                sanphammoi = d.sanphammoi,
                                tenmay = d.tenmay,
                                thumbnail = d.thumbnail
                            }).ToList();
                        else
                            result = db.O_CUNGs
                            .Select(d => new spa_LayDanhSachSPResult
                            {
                                id = d.ID,
                                giaban = d.giaban,
                                sanphamhot = d.sanphamhot,
                                sanphammoi = d.sanphammoi,
                                tenmay = d.tenmay,
                                thumbnail = d.thumbnail
                            }).ToList();
                        break;
                    case 8:
                        if (supplier != 0)
                            result = db.PHU_KIENs.Where(d => d.nhasanxuat == supplier)
                            .Select(d => new spa_LayDanhSachSPResult
                            {
                                id = d.ID,
                                giaban = d.giaban,
                                sanphamhot = d.sanphamhot,
                                sanphammoi = d.sanphammoi,
                                tenmay = d.tenmay,
                                thumbnail = d.thumbnail
                            }).ToList();
                        else
                            result = db.PHU_KIENs
                            .Select(d => new spa_LayDanhSachSPResult
                            {
                                id = d.ID,
                                giaban = d.giaban,
                                sanphamhot = d.sanphamhot,
                                sanphammoi = d.sanphammoi,
                                tenmay = d.tenmay,
                                thumbnail = d.thumbnail
                            }).ToList();
                        break;
                    default:
                        result = db.MAY_ANHs
                            .Select(d => new spa_LayDanhSachSPResult
                            {
                                id = d.ID,
                                giaban = d.giaban,
                                sanphamhot = d.sanphamhot,
                                sanphammoi = d.sanphammoi,
                                tenmay = d.tenmay,
                                thumbnail = d.thumbnail
                            }).ToList();
                        break;
                }

                if (name != "")
                    result = result.Where(c => c.tenmay.ToLower().Contains(name)).ToList();
                if (pfrom != 0)
                {
                    int price = 0;
                    var temp = db.THAM_SO_GIAs.Where(t => t.id == pfrom).Select(t => t.giatu);
                    if (temp.Count() > 0)
                        price = (int)temp.First();
                    result = result.Where(c => c.giaban >= price).ToList();
                }
                if (pto != 0)
                {
                    int price = 0;
                    var temp = db.THAM_SO_GIAs.Where(t => t.id == pto).Select(t => t.giaden);
                    if (temp.Count() > 0)
                        price = (int)temp.First();
                    result = result.Where(c => c.giaban <= price).ToList();
                }
                ListSP.DataSource = result;
                ListSP.DataBind();
                if (result.Count == 0)
                    hasItems = false;
            }
        }
    }
}
